package com.roast.services.my;

import com.roast.util.QueryObject;
import java.util.ArrayList;
import com.roast.domain.*;
import com.roast.util.*;

public class GetUserEventsIMPL extends GeneralSO implements GetUserEvents {

    private long userID;
    private QueryObject queryObject;
    private ArrayList<DomainObject> fromDB;

    @Override
    public int getEvents(ArrayList<Event> list) {
        GetUserEventsIMPL getUserEvents = new GetUserEventsIMPL();
        return getUserEvents.generalExecution(list);
    }

    @Override
    protected int executeSO(Object obj) {
        if (!setUserId(obj)) {
            return 12001;
        }
        if (!setQuery("readMasterEvents")) {
            return 12002;
        }
        if (!readEvents()) {
            return 12003;
        }
        if (!copyEvents(obj)) {
            return 12004;
        }
        if (!setQuery("readStarEvents")) {
            return 12005;
        }
        if (!readEvents()) {
            return 12006;
        }
        if (!copyEvents(obj)) {
            return 12007;
        }
        if (!setQuery("readParticipantsEvents")) {
            return 12008;
        }
        if (!readEvents()) {
            return 12009;
        }
        if (!copyEvents(obj)) {
            return 12010;
        }
        if (!checkResult(obj)) {
            return 12011;
        }
        return 12000;
    }

    private boolean setUserId(Object obj) {
        try {
            @SuppressWarnings("unchecked")
            ArrayList<Event> list = (ArrayList<Event>) obj;
            Event event = list.remove(0);
            userID = event.getMaster().getId();
            if (userID != 0) {
                return true;
            }
        } catch (NullPointerException | ClassCastException ex) {
            System.out.println("[GetUserEvents:setUserId:[" + ex + "]]");
        }
        return false;
    }

    private boolean setQuery(String beanID) {
        queryObject = QueryBeanFactory.getBean(beanID);
        if (queryObject == null) {
            return false;
        }
        return queryObject.addParametar("fuserid", userID);
    }

    private boolean readEvents() {
        try {
            fromDB = dbbr.readMany(queryObject);
        } catch (NullPointerException ex) {
            System.out.println("[GetPublicEvents:readMasterEvent:[" + ex + "]]");
            return false;
        }
        return (queryObject.getSignal() == 10040 || queryObject.getSignal() == 10042);
    }

    @SuppressWarnings("unchecked")
    private boolean copyEvents(Object obj) {
        if (queryObject.getSignal() != 10042) {
            return CopyArrayList.events(fromDB, (ArrayList<Event>) obj);
        }
        return true;
    }

    @SuppressWarnings("unchecked")
    private boolean checkResult(Object obj) {
        return !((ArrayList<Event>) obj).isEmpty();
    }

    @Override
    protected boolean executeState(int signal) {
        switch (signal) {
            case 12000:
                showOperationState(signal, "_getUserEvents: You have successfully get user events from database.");
                return true;
            case 12001:
                showOperationState(signal, "_getUserEvents:ERROR: Fail to set userID.");
                return false;
            case 12002:
                showOperationState(signal, "_getUserEvents:ERROR: Fail to set query readMasterEvents.");
                return false;
            case 12003:
                showOperationState(signal, "_getUserEvents:ERROR: Fail to read master events from database.");
                return false;
            case 12004:
                showOperationState(signal, "_getUserEvents:ERROR: Fail to copy master events.");
                return false;
            case 12005:
                showOperationState(signal, "_getUserEvents:ERROR: Fail to set query readStarEvents.");
                return false;
            case 12006:
                showOperationState(signal, "_getUserEvents:ERROR: Fail to read star events from database.");
                return false;
            case 12007:
                showOperationState(signal, "_getUserEvents:ERROR: Fail to copy star events.");
                return false;
            case 12008:
                showOperationState(signal, "_getUserEvents:ERROR: Fail to set query readParticipantsEvents.");
                return false;
            case 12009:
                showOperationState(signal, "_getUserEvents:ERROR: Fail to read participants events from database.");
                return false;
            case 12010:
                showOperationState(signal, "_getUserEvents:ERROR: Fail to copy participants events.");
                return false;
            case 12011:
                showOperationState(signal, "_getUserEvents:ERROR: No events found.");
                return false;
        }
        return false;
    }
}
